//
//  ConversionRecordDetailViewController.m
//  AppleMarket
//
//  Created by yangy on 13/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "ConversionRecordDetailViewController.h"

// model
#import "TransferAssetModel.h"

@interface ConversionRecordDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSDictionary *itemsDictionary;
@end

@implementation ConversionRecordDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self setNagivationBar];
}

- (void) setNagivationBar
{
    UIButton *rightButton = [UIButton cm_ButtonWithTitle:@"取消划转" font:kFont(13) titleColor:kColorWithHex(0x4A647A)];
    rightButton.titleLabel.textAlignment = NSTextAlignmentRight;
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        // 取消提现
        [self request_CancleWithdraw];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

#pragma mark - 取消提现
- (void) request_CancleWithdraw
{
    [SVProgressHUD show];
    [NetRequest request_DoCancelMutualTransferWithId:self.assetModel.ID completeHandler:^(id  _Nonnull responseObj) {
        if([responseObj objectForKey:@"Status"]){
            [SVProgressHUD showInfoWithStatus:@"互转已取消"];
            [SVProgressHUD dismissWithDelay:3 completion:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }else{
            [SVProgressHUD showInfoWithStatus:[responseObj objectForKey:@"Msg"]];
        }
    } fail:^(NSError * _Nonnull error) {
        [SVProgressHUD showInfoWithStatus:@"网络异常"];
    }];
}


- (void) initUI
{
    [self.view addSubview:self.moneyLabel];
    [self.view addSubview:self.tableView];
    
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.view).offset(40);
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.moneyLabel.mas_bottom).offset(49);
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemsDictionary.allKeys.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.textLabel.font = kFont(14);
        cell.textLabel.textColor = kColorWithHex(0xB5C0CB);
        cell.detailTextLabel.font = kFont(14);
        cell.detailTextLabel.textColor = kColorWithHex(0x0D2B47);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSString *key = _itemsDictionary.allKeys[indexPath.row];
    cell.textLabel.text = key;
    cell.detailTextLabel.text = [self.itemsDictionary objectForKey:key];
    return cell;
}

- (UILabel *)moneyLabel
{
    if(!_moneyLabel){
        NSString *text = self.assetModel.TransferVolume;
        _moneyLabel = [UILabel cm_labelWithText:text font:kFontBold(29) textColor:kColorWithHex(0x0E304C)];
    }
    return _moneyLabel;
}
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [_tableView setRowHeight:20];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    }
    return _tableView;
}

- (NSDictionary *)itemsDictionary
{
    if(!_itemsDictionary){
        _itemsDictionary = @{
                             @"交易类型" : self.assetModel.Direction,
                             @"资产类型" : self.assetModel.CurrencyUnit,
                             @"对方账号" : self.assetModel.TransferInUserId,
                             @"状态" : self.assetModel.TransterStatusName,
                             @"时间" : self.assetModel.CreateTime,
                             @"流水号" : self.assetModel.SerialNumber
                             };
    }
    return _itemsDictionary;
}


@end
